package coint.btc.base.dao;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Set;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import coint.btc.vo.Page;



/**
 * 
 * @ClassName BaseDAO
 * @author BOOM
 * @date 2018/05/18
 *
 */
@SuppressWarnings("unchecked")
@Repository
public abstract class BaseDAO {

	@PersistenceContext
    private EntityManager entityManager;
	
	public Session getSession() {
		return entityManager.unwrap(Session.class);
	}
	
	public EntityManager getEntityManager() {
		return entityManager;
	}

	@Transactional(readOnly = true, rollbackFor = Exception.class)
	public <V> List<V> findByMoreFiled(Class<V> clz, String tableName, LinkedHashMap<String, Object> map) {
		String sql = "select * from "+tableName+" t where 1=1";
		Set<String> set = map.keySet();
		List<String> list = new ArrayList<>(set);
		List<Object> filedList = new ArrayList<Object>();
		for (Object filed : list) {
			sql += " and t."+filed+"=?";
			filedList.add(filed);
		}
		System.out.println(sql);
		SQLQuery addEntity = getSession().createSQLQuery(sql).addEntity(clz);
		for (int i = 0; i < filedList.size(); i++) {
			addEntity.setParameter(i, map.get(filedList.get(i)));

		}
		List<V> result= addEntity.list();
		return result;
	}

	@Transactional(readOnly = true, rollbackFor = Exception.class)
	public <V> List<V> findByMoreFiledPage(Class<V> clz,String tableName, LinkedHashMap<String, Object> map, int rows, int page) {
		String sql = "select * from "+tableName+" t where 1=1";
		Set<String> set = map.keySet();
		List<String> list = new ArrayList<>(set);
		List<Object> filedList = new ArrayList<Object>();
		for (Object filed : list) {
			sql += " and t."+filed+"=?";
			filedList.add(filed);
		}
		SQLQuery addEntity = getSession().createSQLQuery(sql).addEntity(clz);
		for (int i = 0; i < filedList.size(); i++) {
			addEntity.setParameter(i, map.get(filedList.get(i)));
		}
		addEntity.setFirstResult((rows - 1) * page);
		addEntity.setMaxResults(page);
		List<V> result= addEntity.list();
		return result;
	}

	@Transactional(readOnly = true, rollbackFor = Exception.class)
	public Object findCount(String tableName, LinkedHashMap<String, Object> map) {
		String sql = "select count(*) from "+tableName+" t where 1=1";
		Set<String> set = map.keySet();
		List<String> list = new ArrayList<>(set);
		List<Object> filedList = new ArrayList<Object>();
		for (Object filed : list) {
			sql += " and t."+filed+"=?";
			filedList.add(filed);
		}
		SQLQuery createSQLQuery = getSession().createSQLQuery(sql);
		for (int i = 0; i < filedList.size(); i++) {
			createSQLQuery.setParameter(i, map.get(filedList.get(i)));
		}
		return createSQLQuery.uniqueResult();
	}
	
	@Transactional(readOnly = true, rollbackFor = Exception.class)
	public <V> Page<V> findPageCount(Class<V> clz,String tableName, LinkedHashMap<String, Object> map, Integer rows, Integer page) {
		String sql = "select sql_calc_found_rows * from "+tableName+" t where 1=1";
		Set<String> set = map.keySet();
		List<String> list = new ArrayList<>(set);
		List<Object> filedList = new ArrayList<Object>();
		for (Object filed : list) {
			sql += " and t."+filed+"=?";
			filedList.add(filed);
		}
		SQLQuery addEntity = getSession().createSQLQuery(sql).addEntity(clz);
		for (int i = 0; i < filedList.size(); i++) {
			addEntity.setParameter(i, map.get(filedList.get(i)));
		}
		addEntity.setFirstResult((rows - 1) * page);
		addEntity.setMaxResults(page);
		List<V> result= addEntity.list();
		Object uniqueResult = getSession().createSQLQuery("select found_rows()").uniqueResult();
		Page<V> pages = new Page<V>();
		pages.setResults(result);
		pages.setTotal(Integer.parseInt(uniqueResult.toString()));
		return pages;
	}
}
